package com.xiangty.ssmtemplate.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangty.ssmtemplate.common.Result;
import com.xiangty.ssmtemplate.entity.User;
import com.xiangty.ssmtemplate.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Classname UserController
 * @Created by xiangty
 */
@Controller
public class UserController {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/userPage")
    public String userPage(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        Integer pageSize = 3;
        // 获取第几页，3条内容，默认查询总数count
        PageHelper.startPage(pn, pageSize);
        List<User> userList = userService.queryUserList();
        //用PageInfo对结果进行包装, 页数展示5条数据
        PageInfo page = new PageInfo(userList, 5);
        model.addAttribute("pageInfo", page);
        return "user";
    }


    @RequestMapping(value = "/saveUser", method = RequestMethod.POST)
    @ResponseBody
    public Result saveUser(User user) {
        User userInfo = userService.getUserByName(user.getUsername());
        if (userInfo != null) {
            return Result.error("用户名已存在，新增用户失败");
        }

        if (userService.saveUser(user) > 0) {
            return Result.ok("新增用户成功");
        } else {
            return Result.error("新增用户失败");
        }
    }

    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public Result deleteById(@RequestParam(value = "id") Integer id) {
        if (userService.deleteById(id) > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @RequestMapping(value = "/getUserById")
    @ResponseBody
    public Result getUserById(@RequestParam(value = "id") Integer id) {
        User user = userService.getUserById(id);
        if (user != null) {
            return Result.ok(user);
        } else {
            return Result.error("没有用户信息");
        }
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    public Result updateUser(User user) {
        User userInfo = userService.getUserByName(user.getUsername());
        if (userInfo != null) {
            return Result.error("用户名已存在，新增用户失败");
        }

        if (userService.updateUser(user) > 0) {
            return Result.ok("修改用户成功");
        } else {
            return Result.error("修改用户失败");
        }
    }


}
